package com.wsc.consumer.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;

@EnableBinding(Barista.class)
@Component
public class RabbitReceiver {

    /**
     * 接收
     * @param message
     */
    @StreamListener(Barista.INPUT_CHANNEL)
    public void receiver(Message message) throws IOException {
        //得到 channel
        Channel channel = (Channel) message.getHeaders().get(AmqpHeaders.CHANNEL);
        //得到 deliveryTag
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        System.err.println("----------------------------------");
        System.err.println("receiver 接收到消息："+message.getPayload());
        System.err.println("消费完毕----------");
        //手动确认消息
        channel.basicAck(deliveryTag, false);

    }

}
